package com.fsnbigdata.web.deserializer;
import com.fsnbigdata.domain.Ecosystem;
import com.fsnbigdata.service.api.EcosystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jackson.JsonObjectDeserializer;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.convert.ConversionService;
import org.springframework.roo.addon.web.mvc.controller.annotations.config.RooDeserializer;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import io.springlets.web.NotFoundException;
import org.springframework.boot.jackson.JsonComponent;

/**
 * = EcosystemDeserializer
 *
 * TODO Auto-generated class documentation
 *
 */
@RooDeserializer(entity = Ecosystem.class)
@JsonComponent
public class EcosystemDeserializer extends JsonObjectDeserializer<Ecosystem> {

    /**
     * TODO Auto-generated attribute documentation
     *
     */
        private EcosystemService ecosystemService;

        /**
         * TODO Auto-generated attribute documentation
         *
         */
        private ConversionService conversionService;

        /**
         * TODO Auto-generated constructor documentation
         *
         * @param ecosystemService
         * @param conversionService
         */
    @Autowired
    public EcosystemDeserializer(@Lazy EcosystemService ecosystemService, ConversionService conversionService) {
            this.ecosystemService = ecosystemService;
            this.conversionService = conversionService;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return EcosystemService
     */
    public EcosystemService getEcosystemService() {
        return ecosystemService;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ecosystemService
     */
    public void setEcosystemService(EcosystemService ecosystemService) {
        this.ecosystemService = ecosystemService;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return ConversionService
     */
    public ConversionService getConversionService() {
        return conversionService;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param conversionService
     */
    public void setConversionService(ConversionService conversionService) {
        this.conversionService = conversionService;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param jsonParser
     * @param context
     * @param codec
     * @param tree
     * @return Ecosystem
     * @throws IOException
     */
    public Ecosystem deserializeObject(JsonParser jsonParser, DeserializationContext context, ObjectCodec codec, JsonNode tree) {
        String idText = tree.asText();
        Long id = conversionService.convert(idText, Long.class);
        Ecosystem ecosystem = ecosystemService.findOne(id);
        if (ecosystem == null) {
            throw new NotFoundException("Ecosystem not found");
        }
        return ecosystem;
    }
}
